﻿#ifdef WIN32
#pragma execution_character_set("utf-8")
#endif

#include "DMainView.h"

#include <QHBoxLayout>
#include <QVBoxLayout>

DMainView::DMainView(QWidget* parent)
    : QWidget(parent)
{
    setWindowTitle("作业管理系统");

    m_treeWgt = new QTreeWidget();
    m_treeWgt->clear();
    m_treeWgt->setHeaderHidden(true);

    m_areaResult = new QScrollArea();
    m_areaResult->setWidgetResizable(true);
    m_areaResult->setWidget(new QWidget());

    m_layoutContent = new QVBoxLayout();
    m_areaResult->widget()->setLayout(m_layoutContent);

    QHBoxLayout* layoutMain = new QHBoxLayout();
    m_dtStart = new QDateEdit(QDate::currentDate());
    m_dtEnd = new QDateEdit(QDate::currentDate());

    // left
    QHBoxLayout* layoutAction = new QHBoxLayout();
    layoutAction->addWidget(m_dtStart);
    layoutAction->addWidget(m_dtEnd);

    QPushButton* btnQuery = new QPushButton("搜索");
    connect(btnQuery, &QPushButton::clicked, [this] { emit signalQuery(); });
    layoutAction->addWidget(btnQuery);

    QPushButton* btnAdd = new QPushButton("录入");
    connect(btnAdd, &QPushButton::clicked, [this] { emit signalAdd(); });
    layoutAction->addWidget(btnAdd);
    layoutAction->addStretch();

    QVBoxLayout* layoutLeft = new QVBoxLayout();
    layoutLeft->addLayout(layoutAction);
    layoutLeft->addWidget(m_treeWgt);

    layoutMain->addLayout(layoutLeft, 0);
    layoutMain->addWidget(m_areaResult, 1);

    setLayout(layoutMain);

    showMaximized();
}

std::vector<QTreeWidgetItem*> DMainView::getItems(DNodeType type, bool bChild)
{
    std::vector<QTreeWidgetItem*> vecRet;

    const int nCount = m_treeWgt->topLevelItemCount();

    for (int i = 0; i < nCount; ++i)
    {
        QTreeWidgetItem* itemTop = m_treeWgt->topLevelItem(i);
        if (itemTop->data(0, Qt::UserRole).toInt() != type)
        {
            continue;
        }

        if (bChild)
        {
            for (int j = 0; j < itemTop->childCount(); ++j)
            {
                QTreeWidgetItem* itemChild = itemTop->child(j);
                vecRet.push_back(itemChild);
            }
        }
        else
        {
            vecRet.push_back(itemTop);
        }
    }

    return vecRet;
}

std::tuple<QDate, QDate> DMainView::getDate()
{
    return std::make_tuple(m_dtStart->date(), m_dtEnd->date());
}
